package com.tianjipms.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tianjipms.entity.Account;
import com.tianjipms.service.AccountService;
import com.tianjipms.utils.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;

@RestController
@RequestMapping("/api/account")
public class AccountController {
    @Resource
    private AccountService accountService;
    //查询账户
    @GetMapping("/findOne")
    public Result findOne(@RequestParam Long userId){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userId);
        return Result.ok(accountService.getOne(queryWrapper));
    }
    //充值
    @PostMapping("/topUp")
    public Result topUp(@RequestBody Account account){
        if(account.getTopUpPrice().compareTo(BigDecimal.valueOf(1)) == -1){
            return Result.error().message("充值金额太小,无法充值");
        }else {
            account.setBalance(account.getBalance().add(account.getTopUpPrice()));
            accountService.updateById(account);
        }
        return Result.ok();
    }
}
